/* Created on 2006-9-19 */
/**
 *
 */
package com.xbang.core.web.loader;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * @author dev.zhang
 */
public class ServletContextLoaderListener implements ServletContextListener {

	private static final Log logger = LogFactory.getLog(ServletContextLoaderListener.class);

	private ServletContextLoader servletContextLoader;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent event) {
		this.servletContextLoader = createServletContextLoader(event);
		this.servletContextLoader.initServletContext(event.getServletContext());
	}

	/**
	 * @param event
	 * @return Returns the ServletContextLoader
	 */
	private ServletContextLoader createServletContextLoader(ServletContextEvent event) {
		ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
		return (ServletContextLoader) applicationContext.getBean("servletContextLoader");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent event) {
		this.servletContextLoader.closeServletContext(event.getServletContext());
	}

}
